package com.cnzhizi.wheelpop.activity;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.cnzhizi.wheelpop.R;
import com.cnzhizi.wheelpop.model.OneModel;
import com.cnzhizi.wheelpop.model.ThreeModel;
import com.cnzhizi.wheelpop.model.TwoModel;

import java.util.ArrayList;
import java.util.List;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.ListWheelAdapter;


public class MainActivityBackMy extends Activity implements OnClickListener, OnWheelChangedListener {
    private WheelView mWheelOne;
    private WheelView mWheelTwo;
    private WheelView mWheelThree;
    private Button mBtnConfirm;


    /**
     * 所有一级  内含所有数据
     */
    List<OneModel> oneModelList = new ArrayList<OneModel>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_three);
        initViews();
        initListener();
        initData();
    }


    private void initViews() {
        mWheelOne = (WheelView) findViewById(R.id.id_one);
        mWheelTwo = (WheelView) findViewById(R.id.id_two);
        mWheelThree = (WheelView) findViewById(R.id.id_three);
        mBtnConfirm = (Button) findViewById(R.id.btn_confirm);
    }

    private void initListener() {
        // 添加change事件
        mWheelOne.addChangingListener(this);
        // 添加change事件
        mWheelTwo.addChangingListener(this);
        // 添加change事件
        mWheelThree.addChangingListener(this);
        // 添加onclick事件
        mBtnConfirm.setOnClickListener(this);
    }

    private void initData() {
        //数据格式要求，二三级都必须有数据 可用空值代替
        //假数据 1

        OneModel oneModel1 = new OneModel();
        oneModel1.setId("1");
        oneModel1.setName("山东");



        //假数据 2
        List<TwoModel> twoModelList2 = new ArrayList<TwoModel>();
        OneModel oneModel2 = new OneModel();
        oneModel2.setId("1");
        oneModel2.setName("河北");

        TwoModel twoModel2 = new TwoModel();
        twoModel2.setId("4");
        twoModel2.setName("某地");

        TwoModel twoModel3 = new TwoModel();
        twoModel3.setId("5");
        twoModel3.setName("哈哈");

        List<ThreeModel> threeModelList = new ArrayList<ThreeModel>();
        ThreeModel threeModel = new ThreeModel();
        threeModel.setId("6");
        threeModel.setName("哈哈的下一级");
        threeModelList.add(threeModel);

        twoModel3.setThreeModelList(threeModelList);


        twoModelList2.add(twoModel2);
        twoModelList2.add(twoModel3);


        oneModel2.setTwoModelList(twoModelList2);


        oneModelList.add(oneModel1);
        oneModelList.add(oneModel2);


        //处理空值问题
        for(int i=0;i<oneModelList.size();i++){
            List<TwoModel> twoModelListTemp = oneModelList.get(i).getTwoModelList();//获取第二级别
            if (twoModelListTemp == null) {//二级级为空的默认值
                twoModelListTemp = new ArrayList<TwoModel>();
                TwoModel twoModelTemp = new TwoModel();
                twoModelTemp.setName("");
                twoModelTemp.setId("8888");
                twoModelListTemp.add(twoModelTemp);
                //将此数据统一加到大数据集合中
                oneModelList.get(i).setTwoModelList(twoModelListTemp);
            }
            for (int j=0;j<twoModelListTemp.size();j++){
                List<ThreeModel> threeModelListTemp = oneModelList.get(i).getTwoModelList().get(j).getThreeModelList();//获取第三级别

                if (threeModelListTemp == null) {//三级为空的默认值
                    threeModelListTemp = new ArrayList<ThreeModel>();
                    ThreeModel threeModelTemp = new ThreeModel();
                    threeModelTemp.setId("9999");
                    threeModelTemp.setName("");
                    threeModelListTemp.add(threeModelTemp);
                    //将此数据统一加到大数据集合中
                    oneModelList.get(i).getTwoModelList().get(j).setThreeModelList(threeModelListTemp);
                }
            }


        }


        mWheelOne.setViewAdapter(new ListWheelAdapter<String>(MainActivityBackMy.this, oneModelList));
        // 设置可见条目数量
        mWheelOne.setVisibleItems(7);
        mWheelTwo.setVisibleItems(7);
        mWheelThree.setVisibleItems(7);
        updateTwoWheelData();
        updateThreeWheelData();
    }

    @Override
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        // TODO Auto-generated method stub
        if (wheel == mWheelOne) {
            updateTwoWheelData();
        } else if (wheel == mWheelTwo) {
            updateThreeWheelData();
        } else if (wheel == mWheelThree) {

        }
        getSelectedData();
    }

    /**
     * 获取选中的数据的值和信息
     */
    private void getSelectedData() {
        int pCurrent = mWheelOne.getCurrentItem();
        int pTwoCurrent = mWheelTwo.getCurrentItem();
        int pThreeCurrent = mWheelThree.getCurrentItem();
        Log.d("skm",pCurrent+" "+pTwoCurrent+"  "+pThreeCurrent);
        Toast.makeText(this, "一级：id:" + oneModelList.get(pCurrent).getId() + " name:" + oneModelList.get(pCurrent).getName()+ " 二级：id" + oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getId() + "name:" + oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getName() + " 三级：id" + oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getThreeModelList().get(pThreeCurrent).getId() + "name:" + oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getThreeModelList().get(pThreeCurrent).getName() , Toast.LENGTH_SHORT).show();
//
    }

    /**
     * 根据当前的市，更新区WheelView的信息
     */
    private void updateThreeWheelData() {
        int pCurrent = mWheelOne.getCurrentItem();
        int pTwoCurrent = mWheelTwo.getCurrentItem();

        List<ThreeModel> threeModelList = oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).getThreeModelList();//获取第三级别

        /*if (threeModelList == null) {//三级为空的默认值
            threeModelList = new ArrayList<ThreeModel>();
            ThreeModel threeModel = new ThreeModel();
            threeModel.setId("9999");
            threeModel.setName("");
            threeModelList.add(threeModel);
            //将此数据统一加到大数据集合中
            oneModelList.get(pCurrent).getTwoModelList().get(pTwoCurrent).setThreeModelList(threeModelList);
        }*/
        mWheelThree.setViewAdapter(new ListWheelAdapter<String>(this, threeModelList));
        mWheelThree.setCurrentItem(0);

    }

    /**
     * 根据当前的省，更新市WheelView的信息
     */
    private void updateTwoWheelData() {
        int pCurrent = mWheelOne.getCurrentItem();

        List<TwoModel> twoModelList = oneModelList.get(pCurrent).getTwoModelList();//获取第二级别
    /*    if (twoModelList == null) {//二级级为空的默认值
            twoModelList = new ArrayList<TwoModel>();
            TwoModel twoModel = new TwoModel();
            twoModel.setName("");
            twoModel.setId("8888");
            twoModelList.add(twoModel);
            //将此数据统一加到大数据集合中
            oneModelList.get(pCurrent).setTwoModelList(twoModelList);
        }*/
        mWheelTwo.setViewAdapter(new ListWheelAdapter<String>(this, twoModelList));
        mWheelTwo.setCurrentItem(0);
        updateThreeWheelData();
    }

    @Override
    public void onClick(View v)  {
        
    }






}
